package org.xs.web.util;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

 
public class SMap<S extends String,O extends Object> implements Map<S, O>{
	private Pattern intPt = Pattern.compile("[0-9]+");
	private static final long serialVersionUID = 1L;
	private DecimalFormat df = new DecimalFormat("0.00");
	private Map<S, O> delegate;
	public SMap(){
		delegate = new HashMap<S, O>();
	}
	public SMap(int arg0){
		delegate = new HashMap<S, O>(arg0);
	}
	public SMap(Map<S, O> m){
		delegate = m;
	}
	
	
	public String getS(Object key){
		Object v = get(key);
		return v == null ? null : String.valueOf(v);
	}
	
	public String getS(Object key,String dflt){
		Object v = get(key);
		return v == null ? dflt : String.valueOf(v);
	}
	
	public Integer getI(Object key){
		Object v = get(key);
		if(v == null) return null;
		String s = String.valueOf(v);
		if (intPt.matcher(s).matches()) return Integer.valueOf(s);
		throw new IllegalArgumentException("key:"+key+"不是数字");
	}
	public Integer getI(Object key,Integer dflt){
		Object v = get(key);
		if(v == null) return dflt;
		String s = String.valueOf(v);
		if (intPt.matcher(s).matches()) return Integer.valueOf(s);
		return dflt;
	}
	
	public Double getD(Object key,Double dflt){
		String s = getS(key);
		if(s == null) return dflt;
		try {
			Number number = df.parse(s);
			if(number instanceof Long){
				return new Double((Long)number);
			}
			return (Double)number;
		} catch (ParseException e) {
			return dflt;
		}
	}
	
	public Double getD(Object key){
		return getD(key,null);
	}
	public Long getL(Object key){
		String s = getS(key);
		if(s == null) return null;
		try {
			Number number = df.parse(s);
			if(number instanceof Double){
				return ((Double)number).longValue();
			}
			return (Long)number;
		} catch (ParseException e) {
			return null;
		}
	}
	
	public Long getL(Object key,Long dflt){
		String s = getS(key);
		if(s == null) return dflt;
		try {
			Number number = df.parse(s);
			if(number instanceof Double){
				return ((Double)number).longValue();
			}
			return (Long)number;
		} catch (ParseException e) {
			return dflt;
		}
	}
	public void clear() {
	
	}
	public boolean containsKey(Object key) {
		return delegate.containsKey(key);
	}
	public boolean containsValue(Object value) {
		return delegate.containsValue(value);
	}
	public Set<java.util.Map.Entry<S, O>> entrySet() {
		return delegate.entrySet();
	}
	public O get(Object key) {
		return delegate.get(key);
	}
	public boolean isEmpty() {
		return delegate.isEmpty();
	}
	public Set<S> keySet() {
		return delegate.keySet();
	}
	public O put(S key, O value) {
		return delegate.put(key, value);
	}
	public void putAll(Map<? extends S, ? extends O> m) {
		delegate.putAll(m);
	}
	public O remove(Object key) {
		return delegate.remove(key);
	}
	public int size() {
		return delegate.size();
	}
	public Collection<O> values() {
		return delegate.values();
	}
	
	
}
